//
//  TCM_HomeSystemMessageViewController.swift
//  TCM_TradingProject
//
//  Created by 吕春海 on 2018/8/27.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit
import SDCAlertView

class TCM_HomeSystemMessageViewController: TCM_BaseViewController, UITableViewDelegate,UITableViewDataSource {

    var messageTableView:UITableView!
    let messageTypeArry:[String] = ["供应消息", "拍卖消息", "系统消息"]
    let messageImageArry:[String] = ["TCM_MessageSupply", "TCM_MessageAuction", "TCM_MessageSystem"]
    let messageArry:[String] = ["您需要的10000千克云南自贡20头三七准备...", "有人在三七的拍卖活动中报价超过你啦，请…", "恭喜您，您的认证资料已经审核通过，可以..."]
    let messageDateTArry:[String] = ["2018-07-20", "2018-07-20", "2018-07-20"]
    let messageTimeTArry:[String] = ["21:06:06", "21:06:06", "21:06:06"]
    let servicePhoneStr = "10010"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "消息通知"
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "TCM_CustomerService"), style: .plain, target: self, action: #selector(serviceButtonClick))
        
        setTabelView()

        // Do any additional setup after loading the view.
    }

    func setTabelView() {
        self.messageTableView = UITableView(frame: CGRect(x: 0, y: TCM_NavBarHeight(), width: TCM_ScreenWidth, height: TCM_ScreenHeight - TCM_NavBarHeight()), style:UITableViewStyle.plain)
        self.messageTableView.backgroundColor = TCM_HexRGB(0xF0F0F0)
        self.messageTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        self.messageTableView.showsVerticalScrollIndicator = false
        self.messageTableView.delegate = self
        self.messageTableView.dataSource = self
        
        self.view.addSubview(messageTableView)
        
        self.messageTableView.register(UINib.init(nibName: "TCM_HomeMessageTableViewCell", bundle: nil), forCellReuseIdentifier: "MessageCell")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messageTypeArry.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = (messageTableView.dequeueReusableCell(withIdentifier: "MessageCell", for: indexPath)) as! TCM_HomeMessageTableViewCell
        cell.selectionStyle = .none
        cell.messageTypeLabel.text = messageTypeArry[indexPath.row]
        cell.messageLabel.text = messageArry[indexPath.row]
        cell.messageDateLabel.text = messageDateTArry[indexPath.row]
        cell.messageTimeLabel.text = messageTimeTArry[indexPath.row]
        cell.messageImageView.image = UIImage.init(named: messageImageArry[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let messageVC = TCM_MessageDetailsViewController()
        messageVC.title = messageTypeArry[indexPath.row]
        self.navigationController?.pushViewController(messageVC, animated: true)
    }
    
    
    @objc func serviceButtonClick() {
        let alert = AlertController(attributedTitle: String.attribuateTitle("拨打客服电话"), attributedMessage: String.attribuateMessage(servicePhoneStr))
        alert.addAction(AlertAction(attributedTitle: String.attribuateBack("取消"), style: .normal, handler: { _ in
        }))
        alert.addAction(AlertAction(attributedTitle: String.attribuateOK("确定"), style: .normal, handler: { _ in
            let tel = "tel:".appending(self.servicePhoneStr)
            let callView = UIWebView.init()
            let request = URLRequest.init(url:URL(string:tel)!)
            callView.loadRequest(request)
            self.view.addSubview(callView)
        }))
        alert.present()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
